﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Runtime.Serialization;

namespace CCF20Model
{
    partial class Customer : IDataErrorInfo
    {
        CCF20Entities ccf20Ent;
        public Customer()
        {
            ccf20Ent = new CCF20Entities();
            this.IsTransfered = false;
            this.CreateDate = DateTime.Now;
            //Customer LastCustomer = ccf20Ent.Customers.OrderByDescending(c => c.CustomerID).FirstOrDefault();
            //if (LastCustomer != null)
            //{
            //    this.CustomerNo = LastCustomer.CustomerNo;
            //}
        }

        partial void OnGenderChanged()
        {
            this.RemoveError("Gender");

            if (string.IsNullOrEmpty(this.Gender))
            {
                this.AddError("Gender", "客户性别不可为空");

            }
        }
        partial void OnIsTransferedChanged()
        {
            if ((this.IsTransfered != null) && (this.IsTransfered == false))
            {
                TransferedCustomer tc = TransferedCustomers.Where(c => c.CustomerID == this.CustomerID).FirstOrDefault();
                if (tc != null)
                {
                    tc.DeviceKey = string.Empty;
                    tc.IDCNum = string.Empty;
                }
            }
        }


        partial void OnIdentityNoChanged()
        {
            this.RemoveError("IdentityNo");

            if (string.IsNullOrEmpty(this.IdentityNo))
            {
                this.AddError("IdentityNo", "客户身份证号码不可为空");

            }
            else
            {
                var count = from v in ccf20Ent.Customers
                            where v.IdentityNo.Equals(this.IdentityNo) && v.CustomerID != this.CustomerID
                            select v;
                if (count.Count() > 0)
                    this.AddError("IdentityNo", "身份证号码不可重复");
            }
        }
        partial void OnContactNameChanged()
        {
            this.RemoveError("ContactName");

            if (string.IsNullOrEmpty(this.ContactName))
            {
                this.AddError("ContactName", "联系人姓名不可为空");

            }
        }


        partial void OnCustomerNoChanged()
        {
            this.RemoveError("CustomerNo");

            if (string.IsNullOrEmpty(this.CustomerNo))
            {
                this.AddError("CustomerNo", "客户号码不可为空");

            }
        }
        partial void OnFirstMobileChanged()
        {
            this.RemoveError("FirstMobile");

            if (string.IsNullOrEmpty(this.FirstMobile))
            {
                this.AddError("FirstMobile", "联系号码不可为空");

            }
        }



        partial void OnNameChanged()
        {
            this.RemoveError("Name");

            if ((this.Name == null) || (this.Name == ""))
            {
                this.AddError("Name", "姓名不可为空");
                //this.AddError("LastName", "出错了");
            }
        }

        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion
    }
    partial class Device
    {


        public Device()
        {
            this.InStockDate = DateTime.Now;
        }

        partial void OnDeviceStatusChanged()
        {
            if (this.DeviceStatus == "库存")
            {

                if (this.Vehicle != null)
                {
                    if (this.Vehicle.VehicleMaintenanceRecords != null)
                    {
                        CCF20Entities ccf20Ent = new CCF20Entities();
                        IList<VehicleMaintenanceRecord> vmrList = ccf20Ent.VehicleMaintenanceRecords.Where(m => m.VehicleID == this.VehicleID).ToList();
                        foreach (VehicleMaintenanceRecord vmr in vmrList)
                        {
                            ccf20Ent.VehicleMaintenanceRecords.DeleteObject(vmr);
                        }
                        ccf20Ent.SaveChanges();
                    }
                    if (this.Vehicle.Faults != null)
                    {
                        foreach(Fault f in this.Vehicle.Faults.Where(f=>f.IsActive))
                        {
                            f.IsActive = false;
                            f.CloseDate = DateTime.Now;
                            f.CloseReason = "归库";
                        }
                    }
                    if (this.Vehicle.VehicleMaintenanceHistories != null)
                    {
                        foreach (VehicleMaintenanceHistory vmh in this.Vehicle.VehicleMaintenanceHistories)
                        {
                            vmh.IsActive = false;
                            vmh.CloseDate = DateTime.Now;
                            vmh.CloseReason = "归库";
                        }
                    }
                }
                    
            }

        }

    }



    partial class TransferedCustomer : IDataErrorInfo
    {
        CCF20Entities ccf20Ent;
        public TransferedCustomer()
        {
            ccf20Ent = new CCF20Entities();
        }
        partial void OnDeviceKeyChanged()
        {
            this.RemoveError("DeviceKey");
            if (this.DeviceKey != null)
            {
                Device device = ccf20Ent.Devices.Where(d => d.DeviceKey == this.DeviceKey).FirstOrDefault();
                if (device != null && device.CustomerID != this.CustomerID)
                {
                    this.AddError("DeviceKey", "设备序列号重复");
                }
            }
        }
        partial void OnIDCNumChanged()
        {
            this.RemoveError("IDCNum");
            if (this.IDCNum != null)
            {
                Device device = ccf20Ent.Devices.Where(d => d.IDCSimNumber == this.IDCNum).FirstOrDefault();
                if (device != null && device.CustomerID != this.CustomerID)
                {
                    this.AddError("IDCNum", "设备IDC号重复");
                }
            }
        }
        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion
    }

    partial class Vehicle : IDataErrorInfo
    {
        CCF20Entities ccf20Ent;
        //private int _urgentCount;
        //private int _faultCount;
        //private int _maintenanceCount;
        public global::System.Int32 UrgentCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.Faults.Where(f => f.IsActive && f.FaultType == "撞车" && f.VehicleID == this.VehicleID).Count();
            }


        }

        public global::System.Int32 ConfirmedFaultCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.Faults.Where(f => f.IsActive && f.FaultType == "故障" && f.VehicleID == this.VehicleID && f.IsConfirmed).Count();
            }

        }
        public global::System.Int32 FaultCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.Faults.Where(f => f.IsActive && f.FaultType == "故障" && f.VehicleID == this.VehicleID).Count();
            }

        }

        public global::System.Int32 UnConfirmedFaultCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.Faults.Where(f => f.IsActive && f.FaultType == "故障" && f.VehicleID == this.VehicleID && !f.IsConfirmed).Count();
            }

        }
        public global::System.Int32 MaintenanceCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.VehicleMaintenanceHistories.Where(f => f.IsActive && f.VehicleID == this.VehicleID).Count();
            }

        }
        string _SMSStatus = "未发送";
        public global::System.String SMSStatus
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                int count = ccf20Ent.SMSHistories.Where(s => s.IsActive && s.VehicleID == this.VehicleID).Count();
                if (count > 0)
                    _SMSStatus = "已发送";
                return _SMSStatus;
            }

        }

        public Vehicle()
        {
            this.SaleDate = DateTime.Now;
            ccf20Ent = new CCF20Entities();
                
            //ccf20Ent.Faults.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
        }
        partial void OnVehicleTypeIDChanged()
        {
            this.RemoveError("VehicleTypeID");
            if ((this.VehicleTypeID == null) || (this.VehicleTypeID == 0))
            {
                this.AddError("VehicleTypeID", "车辆型号不可为空");
            }
        }
        partial void OnVehicleLicenseChanged()
        {
            this.RemoveError("VehicleLicense");

            if (string.IsNullOrEmpty(this.VehicleLicense))
            {
                this.AddError("VehicleLicense", "车牌号不可为空");
            }
            else
            {
                var count = from v in ccf20Ent.Vehicles
                            where v.VehicleLicense.Equals(this.VehicleLicense) && v.VehicleID != this.VehicleID
                            select v;


                if (count.Count() > 0)
                    this.AddError("VehicleLicense", "车牌号不可重复");
            }
        }
        partial void OnChassiNoChanged()
        {
            this.RemoveError("ChassiNo");

            if (string.IsNullOrEmpty(this.ChassiNo))
            {
                this.AddError("ChassiNo", "底盘号不可为空");

            }
            else
            {
                var count = from v in ccf20Ent.Vehicles
                            where v.ChassiNo.Equals(this.ChassiNo) && v.VehicleID != this.VehicleID
                            select v;


                if (count.Count() > 0)
                    this.AddError("ChassiNo", "底盘号不可重复");
            }
        }

        partial void OnEngineNoChanged()
        {
            this.RemoveError("EngineNo");

            if (string.IsNullOrEmpty(this.EngineNo))
            {
                this.AddError("EngineNo", "引擎号不可为空");

            }
            else
            {
                var count = from v in ccf20Ent.Vehicles
                            where v.EngineNo.Equals(this.EngineNo) && v.VehicleID != this.VehicleID
                            select v;


                if (count.Count() > 0)
                    this.AddError("EngineNo", "引擎号不可重复");
            }
        }


        partial void OnSaleDateChanged()
        {
            this.RemoveError("SaleDate");

            if (SaleDate == null)
            {
                this.AddError("SaleDate", "销售日期不可为空");

            }
        }
        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion
    }
}
